package com.qfedu.fmmall.controller;

import com.qfedu.fmmall.entity.Users;
import com.qfedu.fmmall.service.UserService;
import com.qfedu.fmmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author liuyadi
 */
@RestController
@RequestMapping("/user")
@Api(value = "提供用户的登录和注册功能", tags = "用户管理")
@CrossOrigin
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "username", value = "用户登录账号", required = true),
            @ApiImplicitParam(dataType = "String", name = "password", value = "用户登录密码", required = true)
    })
    @GetMapping(value = "/login")
    public ResultVO login(
            @RequestParam("username") String username,
            @RequestParam(value = "password") String password) {
        ResultVO loginResultVO = userService.checkLogin(username, password);
        return loginResultVO;
    }

    @ApiOperation(value = "用户注册接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "username", value = "用户注册账号", required = true),
            @ApiImplicitParam(dataType = "String", name = "password", value = "用户注册密码", required = true)
    })
    @PostMapping(value = "/regist")
    public ResultVO regist(@RequestBody Users user) {
        ResultVO registResultVO = userService.userResgit(user.getUsername(), user.getPassword());
        return registResultVO;
    }
}
